{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../../')\n",
    "\n",
    "from fedlab.contrib.dataset import RCV1\n",
    "from fedlab.utils.dataset import RCV1Partitioner\n",
    "from fedlab.utils.functional import partition_report\n",
    "\n",
    "import torch\n",
    "from torch.utils.data import DataLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = 2021\n",
    "root = \"../../../../data/RCV1/\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RCV1 Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Try to down load ../../../../data/RCV1/rcv1_train.binary.bz2 from https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/rcv1_train.binary.bz2 ...\n",
      "Source file download done.\n",
      "Load original train set from ../../../../data/RCV1/rcv1_train.binary.bz2.\n",
      "Split original train set into 0.75train:0.25test ...\n",
      "Train/test split done.\n",
      "Train/test save done.\n",
      "npy files already existed.\n"
     ]
    }
   ],
   "source": [
    "# npy file for train split is almost 2.8GB\n",
    "trainset = RCV1(root,\n",
    "              train=True,\n",
    "              download=True,\n",
    "              generate=True,\n",
    "              seed=seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train data size: 15181\n",
      "Train feature number: 47236\n",
      "Train class distribution: class0:class1 = 7860:7321\n"
     ]
    }
   ],
   "source": [
    "print(f\"Train data size: {len(trainset)}\")\n",
    "print(f\"Train feature number: {trainset.data.shape[1]}\")\n",
    "print(f\"Train class distribution: class0:class1 = \"\n",
    "      f\"{int(trainset.targets.sum())}:{len(trainset)-trainset.targets.sum()}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "npy files already existed.\n"
     ]
    }
   ],
   "source": [
    "testset = RCV1(root,\n",
    "               train=False,\n",
    "               download=False,\n",
    "               generate=False,\n",
    "               seed=seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test data size: 5061\n",
      "Test feature number: 47236\n",
      "Test class distribution: class0:class1 = 2631:2430\n"
     ]
    }
   ],
   "source": [
    "print(f\"Test data size: {len(testset)}\")\n",
    "print(f\"Test feature number: {testset.data.shape[1]}\")\n",
    "print(f\"Test class distribution: class0:class1 = \"\n",
    "      f\"{int(testset.targets.sum())}:{len(testset)-testset.targets.sum()}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Partition\n",
    "\n",
    "`DataPartitioner` for RCV1 is `RCV1Partitioner`. It is similar with `AdultPartitioner`. Please refer to [Adult Tutorial](../adult/adult_tutorial.ipynb) for more details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf_torch]",
   "language": "python",
   "name": "conda-env-tf_torch-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
